package com.lxc.demos.view;

import com.lxc.demos.SampleApplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;

public class TabView extends View {
	public static String TAG = SampleApplication.instance.TAG; 
	
	private int currentIndex = 0;
	private int totalNum = 0;
	
	private int cx = 10;
	private int cy = 10;
	private int radius = 10;
	private int currentX;
	
	private int backgroundColor;  //圆圈的背景颜色
	private int textColor;        //文字颜色
	private float textSize;         //文字大小
	
	
	private Paint paint ;
	
	private int mAscent;

	public TabView(Context context) {
		super(context);
		initTabView();
		Log.d(TAG,"<--TabView(Context context)-->");
	}

	public TabView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initTabView();
		Log.d(TAG,"<--TabView(Context context, AttributeSet attrs, int defStyle)-->");
	}

	public TabView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initTabView();
		Log.d(TAG,"<--TabView(Context context, AttributeSet attrs)-->");
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.d(TAG,"<--TabView.onDraw("+canvas+")-->");
		if(currentIndex > totalNum)
			currentIndex = currentIndex%totalNum;
		for(int i = 0; i < totalNum; i++){
			currentX = cx+cx*4*i;
			if((i+1) == currentIndex){
				canvas.drawCircle(currentX, cy, 2*radius, paint);
				paint.setColor(textColor);
				canvas.drawText(""+currentIndex,currentX, cy, paint);
				paint.setColor(backgroundColor);
			}else{
				canvas.drawCircle(currentX, cy, radius, paint);
			}

		}
	}
	
	public void setValue(int currentIndex,int totalNum){
		Log.d(TAG,"<--TabView.setValue("+currentIndex+","+totalNum+")-->");
		this.currentIndex = currentIndex;
		this.totalNum = totalNum;
		invalidate();
	}
	
	/**
	 * 设置一些初始化的值
	 */
	private final void initTabView(){
		backgroundColor = 0xFFFFFFFF;
		textColor = 0xFF000000;
		textSize = radius;
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(backgroundColor);
		paint.setTextSize(textSize);
	}
	
	/**
	 * Called to determine the size requirements for this view and all of its
	 * children. 　　
	 * 一个MeasureSpec封装了父布局传递给子布局的布局要求，每个MeasureSpec代表了一组宽度和高度的要求。
	 * 一个MeasureSpec由大小和模式组成 。
	 * 它有三种模式：UNSPECIFIED(未指定),父元素部队自元素施加任何束缚，子元素可以得到任意想要的大小
	 * ；EXACTLY(完全)，父元素决定自元素的确切大小
	 * ，子元素将被限定在给定的边界里而忽略它本身大小；AT_MOST(至多)，子元素至多达到指定大小的值。
	 *　　它常用的三个函数： 　　
	 * 1.static int getMode(int	measureSpec):根据提供的测量值(格式)提取模式(上述三个模式之一) 　　
	 * 2.static int getSize(int measureSpec):根据提供的测量值(格式)提取大小值(这个大小也就是我们通常所说的大小) 　　
	 * 3.static int makeMeasureSpec(int size,int mode):根据提供的大小值和模式创建一个测量值(格式)
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		Log.d(TAG,"<--TabView.onMeasure("+widthMeasureSpec+","+heightMeasureSpec+")-->");
		int width = measureWidth(widthMeasureSpec);
		int height = measureHeight(heightMeasureSpec);
        setMeasuredDimension(width,height);
		Log.d(TAG,"<--TabView.setMeasuredDimension("+width+","+height+")-->");
	}

	/**
	 * Called after a view and all of its children has been inflated from XML.
	 */
	@Override
	protected void onFinishInflate() {
		Log.d(TAG,"<--SlipView.onFinishInflate()-->");
		super.onFinishInflate();
	}

	/**
	 * Called when this view should assign a size and position to all of its children.
	 */
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		Log.d(TAG,"<--SlipView.onLayout("+changed+","+left+","+top+","+right+","+bottom+")-->");
		super.onLayout(changed, left, top, right, bottom);
	}
	
    /**
     * Determines the width of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text
            result = 4*radius*2*radius + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {//至多这么大
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }

        return result;
    }
    
    /**
     * Determines the height of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text (beware: ascent is a negative number)
            result = 4*radius + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {//至多这么大
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
	
	
	
	

}
